trigger SyncHospitalApprove on Hospital__c (before delete,after insert,after update) {
   boolean tf;
   list<hospital_approve__c> halup=new list<hospital_approve__c>();
   list<hospital__c> hosid=new list<hospital__c>();
   list<hospital_approve__c> halin=new list<hospital_approve__c>();
   if(system.trigger.isinsert)tf=true;
   if(system.trigger.isupdate){tf=trigger.oldmap.get(trigger.new[0].id).classchangeto__c!=trigger.new[0].classchangeto__c ||
       trigger.oldmap.get(trigger.new[0].id).approvalstatus__c!=trigger.new[0].approvalstatus__c ||
       trigger.oldmap.get(trigger.new[0].id).approvaltype__c!=trigger.new[0].approvaltype__c ||
       trigger.oldmap.get(trigger.new[0].id).ownerchangeto__c!=trigger.new[0].ownerchangeto__c ||
       trigger.oldmap.get(trigger.new[0].id).namechangeto__c!=trigger.new[0].namechangeto__c||
       trigger.oldmap.get(trigger.new[0].id).DoorNo__c!=trigger.new[0].DoorNo__c
       ;}
    if(system.trigger.isdelete)tf=true;

   if(tf){
    if(system.trigger.isinsert||system.trigger.isupdate){
       
        list<id> idl=new list<id>();
        for(hospital__c hospid:trigger.new){
            idl.add(hospid.id);
        }
        list<hospital_approve__c> hal=[select name,hospital__c,owner__c from hospital_approve__c where hospital__c in:idl];
        list<id> hospitalid=new list<id>();
        for(hospital_approve__c haid:hal){
            hospitalid.add(haid.hospital__c);
        }
        list<hospital__c> hoslist=[select id from hospital__c where id not in:hospitalid limit 10];
        for(hospital__c hp:trigger.new){
     
              if(system.trigger.isinsert){
                    hospital_approve__c ha=new hospital_approve__c(name=hp.name,hospital__c=hp.id,owner__c=hp.ownerid);
                    halin.add(ha);
               }
               else if(system.trigger.isupdate){
                   if(system.trigger.oldmap.get(hp.id).manager__c==hp.manager__c){
          //        list<hospital_approve__c> hal=[select name,hospital__c,owner__c from hospital_approve__c where hospital__c=:hp.id];
                   for(hospital_approve__c happ:hal){
                       if(happ.hospital__c==hp.id){
                            happ.owner__c=hp.ownerid;
                            happ.name=hp.name;
                            halup.add(happ);
                       }
                       }
                   }
                    for(hospital__c h:hoslist){
                      if(h.id==hp.id){
                    hospital_approve__c ha=new hospital_approve__c(name=hp.name,hospital__c=hp.id,owner__c=hp.ownerid);
                    halin.add(ha);
                        }
                      }    
                    }
               }     
        update halup;
        insert halin;
    
        }else if(system.trigger.isdelete){
            list<id> idl=new list<id>();
            for(hospital__c hpid:trigger.old){
                idl.add(hpid.id);
            }
            list<hospital_approve__c> ha=new list<hospital_approve__c>();
            ha=[select id from hospital_approve__c where hospital__c in:idl];
            
            if(ha.size()>0){
              delete ha;
            }
            /*
             *  Delete the Hospital budget.
             *  Modified by Shuai Cao
             */
            list<HospitalMonthlyAllocation__c> HMA =[select id from HospitalMonthlyAllocation__c where Hospital__c in:idl and ((Year__c =:String.valueof(date.today().Year()) and Month__c >: String.valueof(date.today().Month())) or (Year__c >:String.valueof(date.today().Year())))];
            if(HMA.size()>0)
            {
                Delete HMA;
            }
        
        }
    }
}